<template>
  <div>
    <el-dialog :title="info.title" :visible.sync="info.isShow">
      <el-form :model="form">
        <el-form-item label="所属角色" :label-width="formLabelWidth">
          <el-select v-model="form.roleid" placeholder="请选择">
            <el-option
              v-for="item in roles"
              :value="item.id"
              :key="item.id"
              :label="item.rolename"
            >
            </el-option>
          </el-select> 
        </el-form-item>
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="form.password"></el-input>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="form.status"
            active-color="blue"
            inactive-color="grey"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="info.isAdd" @click="confirm">确 定</el-button>
        <el-button type="primary" @click="update" v-else>修 改</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { roleList,adminAdd, adminOne, adminUpdate } from "../../../request/api";
import {successAlert} from "../../../utils/alert"
export default {
  props: ["info"],
  data() {
    return {
      form: {
        roleid: "",
        username: "",
        password: "",
        status: 1, //1.正常  2.禁用
      },
      formLabelWidth: "70px",
      roles: [],
    };
  },
  methods: {
    requestAdmin() {
      roleList().then((res) => {
        if (res.code === 200) {
          this.roles = res.list;
        }
      });
    },
    cancel() {
      this.info.isShow=false;
      this.form={
        roleid: "",
        username: "",
        password: "",
        status: 1, //1.正常  2.禁用
      };
    },
    confirm(){
      adminAdd(this.form).then(res=>{
        console.log(res);
          if(res.code===200){
            successAlert(res.msg);
            this.cancel();
            this.event.$emit("adminList")
          }
      })
    },
    oneDetail(uid){
        adminOne({uid}).then(res=>{
          if(res.code===200){
            console.log(res);
            this.form=res.list;
          }
        })
    },
    update(){
      adminUpdate(this.form).then(res=>{
        if(res.code===200){
          successAlert(res.msg);
          this.cancel();
          this.event.$emit("adminList");
        }
      })
    }
  },
  mounted() {
    this.requestAdmin();
  },
};
</script>

<style>
</style>